//
//  GNStatusPictureView.swift
//  SinaWeChat
//
//  Created by mac on 15/12/11.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

//重用标识符
private let GNStatusPitureViewItemId = "GNStatusPitureViewItemId"

//每一个条目之间的间距
private let itemMargin : CGFloat = 5

//每一个条目的宽高
private let itemWH = (SCREENW - itemMargin * 2 - GNStatusCellMargin * 2) / 3


class GNStatusPictureView: UICollectionView {

    var sizeContraint:Constraint?
    
    //当前配图视图显示的图片数据
    
    var pic_urls: [GNStatusPictureInfo]?{
        
        didSet{
            
            //
            countLabel.text = "\(pic_urls?.count ?? 0)"
            
            //根据图片张图,计算当前控件的大小
            
            let size = calcSize(pic_urls?.count ?? 0)
            
            //设置当前控件的大小
<<<<<<< HEAD
            self.snp_updateConstraints { (make) -> Void in
=======
            self.snp_makeConstraints { (make) -> Void in
>>>>>>> 009be840054e3af11221fb27da600fe2ee052d08
                
                self.sizeContraint = make.size.equalTo(size).constraint
            }
            
            reloadData()
            
        }
        
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
        
        
        //设置collectionView属性
        //注册cell
        
        registerClass(GNStatusPictureViewCell.self, forCellWithReuseIdentifier: GNStatusPitureViewItemId)
        
        //设置数据源
        dataSource = self
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置条目的大小
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        //指定水平方向的间距
        layout.minimumInteritemSpacing = itemMargin
        
        layout.minimumLineSpacing = itemMargin
        
    }
<<<<<<< HEAD
    
 
=======
>>>>>>> 009be840054e3af11221fb27da600fe2ee052d08

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    private func setupUI(){
        
        addSubview(countLabel)
        
        countLabel.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        
    }
    
    //MARK  : - 计算当前控件的大小
    
    private func calcSize(count: Int) -> CGSize{
        
        //计算当前显示的行与列
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count )
        
<<<<<<< HEAD
        let row = count == 4 ? 2 : ((count-1)/3 + 1)
=======
        let row = count == 4 ? 2 : (count >= 3 ? 3 : count)
>>>>>>> 009be840054e3af11221fb27da600fe2ee052d08
        
        
        //宽度 = 列数*没一条目的宽度 + 间隔*(列数-1)
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        
        let height = CGFloat(row) * itemWH + CGFloat(row - 1)*itemMargin
        
       return CGSize(width: width, height: height)
    }
    
    //懒加载控件(显示几个)
    
    private lazy var countLabel:UILabel = UILabel(textColor: UIColor.blackColor(), fontSize: 40)
    
}

//MARK : - UICollectionViewDataSource

extension GNStatusPictureView : UICollectionViewDataSource {
    
    
    //返回有多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pic_urls?.count ?? 0
    }
    
    
    //返回cell
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(GNStatusPitureViewItemId, forIndexPath: indexPath) as! GNStatusPictureViewCell
        
        cell.pictureInfo = pic_urls![indexPath.row]
        return cell
        
    }
    
}





//自定义 cell
class GNStatusPictureViewCell: UICollectionViewCell {
    
    var pictureInfo: GNStatusPictureInfo? {
        
        didSet{
            
            if let urlString = pictureInfo?.thumbnail_pic {
                
                gifImageView.hidden = !urlString.hasSuffix(".gif")
                
                imgView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named:"timeline_image_placeholder"))
                
            }
            
        }
        
    }
    
    override init(frame:CGRect){
        super.init(frame:frame)
        
        setupUI()
        }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        
        backgroundColor = RandomColor()
        
        //添加子空件
        
        contentView.addSubview(imgView)
        contentView.addSubview(gifImageView)
        
        //添加约束
        imgView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            
            
            make.trailing.equalTo(imgView.snp_trailing)
            make.bottom.equalTo(imgView.snp_bottom)
            
            
        }
        
        
        
        
        
    }
    
    
    
    //MARK : - 懒加载
    
    private lazy var imgView: UIImageView = {
        
        let imageView = UIImageView()
        
        //指定内容显示模式
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        
        //超出部分截掉
        imageView.clipsToBounds = true
        
        return imageView
        
        
    }()
    
    //gif 图标
    private lazy var gifImageView: UIImageView = UIImageView(image:UIImage(named:"timeline_image_gif"))
    
}
